package com.huluobo.day11_im.base;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.CompositeDisposable;

public class BasePresenter<V> {
    private WeakReference<V> weakReference;

    protected CompositeDisposable compositeDisposable = new CompositeDisposable();

    public void attachView(V view) {
        if (weakReference == null) {
            weakReference = new WeakReference<>(view);
        }
    }

    public void disAttach() {
        if (weakReference != null) {
            weakReference.clear();
            weakReference = null;
        }

        if (compositeDisposable != null) {
            compositeDisposable.clear();
            compositeDisposable = null;
        }
    }

    public Boolean isAttach() {
        return weakReference != null && weakReference.get() != null;
    }

    public V getView() {
        if (weakReference != null && weakReference.get() != null) {
            return weakReference.get();
        }
        return null;
    }
}
